package com.ccp.dev.system.controller;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.dao.MessageReceiverDao;
import com.ccp.dev.system.dao.MessageReceiverOrgDeleteDao;
import com.ccp.dev.system.dao.SysUserDao;
import com.ccp.dev.system.model.MessageReceiver;
import com.ccp.dev.system.model.MessageReceiverOrgDelete;
import com.ccp.dev.system.model.MessageSend;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.MessageReceiverService;
import com.ccp.dev.system.service.MessageSendService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
 * 对象功能:消息接收者 控制器类
 * 开发人员:dxy
 * 创建时间:2019-08-15 15:13:00
 */
@Controller
@Action(ownermodel= SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM + "/messageReceiver")
public class MessageReceiverController extends SysBaseController {
    @Resource
    private MessageSendService sendService;
    @Resource
    MessageReceiverService receiverService;
    @Resource
    MessageReceiverOrgDeleteDao orgDeleteDao;
    @Resource
    MessageReceiverDao receiverDao;
    @Resource
    SysUserDao userDao;
    private static final String SUCCESS = "operation.success";
    private static final String FAILURE = "operation.failure";

    /**
     * 取得消息接收者分页列表
     * @param request
     * @return ResultData
     */

    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="消息接收者分页列表",detail="查询消息接收者分页列表<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    @ResponseBody
    public ResultData list(HttpServletRequest request)  {
        QueryFilter queryFilter = new QueryFilter(request);
        queryFilter.addFilter("userId", ContextUtil.getCurrentUserId());
        queryFilter.addFilter("orgId", ContextUtil.getCurrentOrgId());
        queryFilter.addFilter("isDelete", MessageReceiver.NOTDELETE);
        try {
            /**
             * 修改已收到消息list页面的逻辑,查询到用户与组织的消息
             */
            List<MessageSend> list = sendService.getReceiverByUser(queryFilter);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess(SUCCESS, queryFilter.getPageBean().getTotalCount(), list);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataSuccess(FAILURE);
        }
    }

    /**
     * 删除消息接收者
     * @param request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @Action(description="删除消息接收者",detail="删除消息接收者<#if success>成功<#else>失败</#if>",exectype = "删除日志")

    @ResponseBody
    public ResultData del(HttpServletRequest request){
        SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
        String[] lAryId = RequestUtil.getStringAryByStr(request, "id");
        try {
            receiverService.delByIdsCascade(lAryId, curUser.getUserId());
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
           return getResultDataError("delete.failure");
        }
    }

    /**
     * 标记消息为已读（设置发送时间字段）
     * @param request
     * @return ResultData
     */
    @RequestMapping("/mark")
    @Action(description="标记信息为已读",detail="标记信息为已读<#if success>成功<#else>失败</#if>",exectype = "编辑日志")
    @ResponseBody
    public ResultData mark(HttpServletRequest request) {
        String[] lAryId = RequestUtil.getStringAryByStr(request, "id");
        try {
            receiverService.updateReadStatus(lAryId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess(SUCCESS);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
             return getResultDataError(FAILURE);
        }
    }

    @RequestMapping("/needPopupRemind")
    @Action(description = "弹框提示收到的消息")
    @ResponseBody
    public ResultData needPopupRemind(){
        return getResultDataSuccess(SUCCESS,receiverService.needPopupRemind());
    }

    /*	                                       页面跳转
     *************************************************************************************************************************/

    /**
     * 接收消息页面
     * @return html文件前缀
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "接收消息明细页面页面跳转", exectype = "页面跳转日志")
    public String viewRunningList() {
        return "messageReceiver/list";
    }
}